package com.gome.util;


import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;

public class MongoClientURI
{
  private static final String PREFIX = "mongodb://";
  final String userName;
  final char[] password;
  final List<String> _hosts;
  final String database;
  final String collection;
  final String _uri;


  MongoClientURI(String uri) {
    this._uri = uri;
    if (!uri.startsWith("mongodb://")) {
      throw new IllegalArgumentException("uri needs to start with mongodb://");
    }
    uri = uri.substring("mongodb://".length());
    int idx = uri.lastIndexOf("/");
    String optionsPart;
    String serverPart;
    String nsPart;
    if (idx < 0) {
      if (uri.contains("?")) {
        throw new IllegalArgumentException("URI contains options without trailing slash");
      }
      serverPart = uri;
      nsPart = null;
      optionsPart = "";
    } else {
      serverPart = uri.substring(0, idx);
      nsPart = uri.substring(idx + 1);
      TraceInfo.traceInfo(serverPart);
      TraceInfo.traceInfo(nsPart);
      idx = nsPart.indexOf("?");
      if (idx >= 0) {
         optionsPart = nsPart.substring(idx + 1);
         nsPart = nsPart.substring(0, idx);
         TraceInfo.traceInfo(optionsPart);
         TraceInfo.traceInfo(nsPart);
      } else {
        optionsPart = "";
      }

    }
    TraceInfo.traceInfo(serverPart);

    List all = new LinkedList();
    TraceInfo.traceInfo(uri);
    idx = serverPart.indexOf("@");
    TraceInfo.traceInfo(idx);
    if (idx > 0) {
      String authPart = serverPart.substring(0, idx);
      serverPart = serverPart.substring(idx + 1);
      TraceInfo.traceInfo(serverPart);
      idx = authPart.indexOf(":");
      this.userName = authPart.substring(0, idx);
      this.password = authPart.substring(idx + 1).toCharArray();
     // TraceInfo.traceInfo("userName",userName);
     // TraceInfo.traceInfo("password",password);
    } else {
      this.userName = null;
      this.password = null;
    }

    Collections.addAll(all, serverPart.split(","));
   
    this._hosts = Collections.unmodifiableList(all);
    TraceInfo.traceInfo(_hosts);
  
    if (nsPart != null) {
       idx = nsPart.indexOf(".");
     
      if (idx < 0) {
    	 
        this.database = nsPart;
        this.collection = null;
      } else {
        this.database = nsPart.substring(0, idx);
        this.collection = nsPart.substring(idx + 1);
        TraceInfo.traceInfo(database);
        TraceInfo.traceInfo(collection);
      }
    } else {
      this.database = null;
      this.collection = null;
    }

  }

  

public static void main(String[] args) {
	MongoClientURI n = new MongoClientURI("mongodb://rb:rb@127.0.0.1:27017/rb");
}

 
 

  public String getUsername()
  {
    return this.userName;
  }

  public char[] getPassword()
  {
    return this.password;
  }

  public List<String> getHosts()
  {
    return this._hosts;
  }

  public String getDatabase()
  {
    return this.database;
  }

  public String getCollection()
  {
    return this.collection;
  }

  public String getURI()
  {
    return this._uri;
  }

 
  public String toString()
  {
    return this._uri;
  }
}
